import { BaseResponse } from '@/common/BaseResponse';
import { Public } from '@/decorators/visit.decorator';
import { Body, Controller, Get, Post, Query, Req } from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiQuery,
  ApiTags,
} from '@nestjs/swagger';
import { DictionaryService } from './dictionary.service';
import { AnswerProblemDto } from './dto/answer-problem.dto';
import { CollectionWordDto } from './dto/collectionw-word.dto';
import { DictionaryWordsDto } from './dto/dictionary-words.dto';
import { DictionaryDto } from './dto/dictionary.dto';
import { RandDataDto } from './dto/rand-data.dto';

@ApiTags('词典管理')
@Controller('dictionary')
export class DictionaryController {
  constructor(private readonly dictionaryService: DictionaryService) {}

  @Public()
  @ApiOperation({
    summary: '查询字典列表',
  })
  @Get('/getDictionaryList')
  getDictionaryList() {
    return this.dictionaryService.getDictionaryList();
  }

  @Public()
  @ApiOperation({
    summary: '增加一个字典',
  })
  @ApiBearerAuth()
  @Post('/addDictionary')
  addDictionary(@Body() dictionaryDto: DictionaryDto) {
    return this.dictionaryService.addDictionary(dictionaryDto);
  }

  @Public()
  @ApiOperation({
    summary: '给字典添加单词',
  })
  @ApiBearerAuth()
  @Post('/addDictionaryWords')
  addDictionaryWords(@Body() dictionaryWordsDto: DictionaryWordsDto) {
    return this.dictionaryService.addDictionaryWords(dictionaryWordsDto);
  }

  @ApiOperation({
    summary: '查询对应字典的词汇列表',
  })
  @ApiBearerAuth()
  // @ApiQuery({
  //   type: Number,
  //   name: 'lastId',
  //   description: '首页第一条数据的id',
  // })
  @ApiQuery({
    type: Number,
    name: 'size',
    description: '每页数',
  })
  @ApiQuery({
    type: Number,
    name: 'current',
    description: '当前页',
  })
  @ApiQuery({
    type: Number,
    name: 'dictionaryId',
    description: '字典id',
  })
  @Get('/getDictionaryWords')
  getDictionaryWords(
    @Req() req,
    @Query('current') current = 1,
    @Query('size') size = 10,
    // @Query('lastId') lastId = 0,
    @Query('dictionaryId') dictionaryId = 0,
  ) {
    return this.dictionaryService.getDictionaryWords(
      {
        current: Number(current),
        size: Number(size),
        // lastId: Number(lastId),
        dictionaryId: Number(dictionaryId),
      },
      req.user.id,
    );
  }

  @ApiOperation({
    summary: '回答一个题目',
  })
  @ApiBearerAuth()
  @Post('/answerOneProblem')
  answerOneProblem(@Req() req, @Body() answerProblemDto: AnswerProblemDto) {
    answerProblemDto.userId = req.user.id;
    return this.dictionaryService.answerOneProblem(answerProblemDto);
  }

  @ApiOperation({
    summary: '批量学习',
  })
  @ApiBearerAuth()
  @Post('/answerMannyProblem')
  answerMannyProblem(
    @Req() req,
    @Body() answerProblemDtoList: Array<AnswerProblemDto>,
  ) {
    answerProblemDtoList = answerProblemDtoList.map((answerProblemDto) => {
      answerProblemDto.userId = req.user.id;
      return answerProblemDto;
    });
    return this.dictionaryService.answerMannyProblem(answerProblemDtoList);
  }

  @ApiOperation({
    summary: '收藏单词',
  })
  @ApiBearerAuth()
  @Post('/collectionWord')
  collectionWord(@Req() req, @Body() collectionWordDto: CollectionWordDto) {
    collectionWordDto.userId = req.user.id;
    return this.dictionaryService.collectionWord(collectionWordDto);
  }

  @ApiOperation({
    summary: '随机获取多条数据',
  })
  @ApiBearerAuth()
  @Post('/getRanDataLimit')
  getRanDataLimit(@Body() randData: RandDataDto) {
    // ORDER BY rand() LIMIT 5
    return this.dictionaryService.getRanDataLimit(randData);
  }

  @ApiOperation({
    summary: '查询我的收藏单词列表',
  })
  @ApiBearerAuth()
  @Get('/getMyCollectionWords')
  getMyCollectionWords(@Req() req) {
    return this.dictionaryService.getMyCollectionWords(Number(req.user.id));
  }

  @ApiOperation({
    summary: '查询我的学习历史',
  })
  @ApiBearerAuth()
  @Get('/getMyStudyWordsHistory')
  getMyStudyWordsHistory(@Req() req) {
    return this.dictionaryService.getMyStudyWordsHistory(Number(req.user.id));
  }

  @ApiOperation({
    summary: '查询我的错题',
  })
  @ApiBearerAuth()
  @Get('/getMyErrorWords')
  getMyErrorWords(@Req() req) {
    return this.dictionaryService.getMyErrorWords(Number(req.user.id));
  }

  @ApiOperation({
    summary: '查询单词详情',
  })
  @ApiBearerAuth()
  @ApiQuery({
    name: 'id',
    description: '动态详情',
    example: 0,
  })
  @Get('/getDetail')
  getDetail(@Query('id') id: number) {
    return this.dictionaryService.getDetail(Number(id));
  }
}
